package api

import (
	"TaskAssignmentSystem/internal/app/services"
	"TaskAssignmentSystem/internal/pkg/middleware/handler"
	"TaskAssignmentSystem/pkg/utils"
	"errors"
	"github.com/kataras/iris/v12"
)

type SendVerificationCodeRequest struct {
	RequestType string `json:"request_type" validate:"required"`
	Email       string `json:"email"`
	Phone       string `json:"phone"`
}

// 发送邮件验证码
func SendEmailCodeHandler(ctx iris.Context) {
	var RequestParams SendVerificationCodeRequest
	if err := ctx.ReadJSON(&RequestParams); err != nil {
		handler.ArgumentErrorHandle(err, ctx)
		return
	}
	var err error
	if RequestParams.RequestType == "1" {
		//邮箱注册验证码
		err = sendEmailVerificationCodeRegister(RequestParams.Email)
	} else if RequestParams.RequestType == "2" {
		//修改密码
		err = sendEmailVerificationCodeResetPwdNotLogin(RequestParams.Email)
	} else if RequestParams.RequestType == "3" {
		//修改邮箱，旧邮箱
		err = sendEmailVerificationCodeEmailResetOldEmail(RequestParams.Email)
	} else if RequestParams.RequestType == "4" {
		//修改邮箱，新邮箱
		err = sendEmailVerificationCodeEmailResetNewEmail(RequestParams.Email)
	} else if RequestParams.RequestType == "5" {
		//修改手机号，旧邮箱
		err = sendEmailVerificationCodePhoneResetOldEmail(RequestParams.Email)
	} else {
		ctx.JSON(utils.ResponseServerError(errors.New("发送邮件验证码类型异常！")))
		return
	}

	if err != nil {
		ctx.JSON(utils.ResponseServerError(err))
		return
	} else {
		ctx.JSON(utils.ResponseOK())
		return
	}
}

func sendEmailVerificationCodeRegister(email string) error {
	var userServe services.SystemUserService
	if exist, err := userServe.VerifyEmail(email); err != nil {
		return err
	} else {
		if exist {
			//邮箱已经存在
			return errors.New("该邮箱已注册！")
		} else {
			//邮箱不存在，则发送邮件验证码
			var emailServe services.EmailService
			if err := emailServe.SendEmailVerificationCodeRegister(email); err != nil {
				return err
			} else {
				return nil
			}
		}
	}
}

func sendEmailVerificationCodeResetPwdNotLogin(email string) error {
	var userServe services.SystemUserService
	if exist, err := userServe.VerifyEmail(email); err != nil {
		return err
	} else {
		if exist {
			//邮箱已经存在
			var emailServe services.EmailService
			if err := emailServe.SendEmailVerificationCodeResetPwd(email); err != nil {
				return err
			} else {
				return nil
			}
		} else {
			//邮箱不存在
			return errors.New("该邮箱未注册!")
		}
	}
}

func sendEmailVerificationCodeEmailResetOldEmail(email string) error {
	var userServe services.SystemUserService
	if exist, err := userServe.VerifyEmail(email); err != nil {
		return err
	} else {
		if exist {
			//邮箱已经存在
			var emailServe services.EmailService
			if err := emailServe.SendVerificationCodeEmailResetOldEmail(email); err != nil {
				return err
			} else {
				return nil
			}
		} else {
			//邮箱不存在
			return errors.New("该邮箱未注册!")
		}
	}
}

func sendEmailVerificationCodeEmailResetNewEmail(email string) error {
	var userServe services.SystemUserService
	if exist, err := userServe.VerifyEmail(email); err != nil {
		return err
	} else {
		if exist {
			//邮箱已经存在
			return errors.New("该邮箱已注册!")
		} else {
			//邮箱不存在
			var emailServe services.EmailService
			if err := emailServe.SendVerificationCodeEmailResetNewEmail(email); err != nil {
				return err
			} else {
				return nil
			}
		}
	}
}

func sendEmailVerificationCodePhoneResetOldEmail(email string) error {
	var userServe services.SystemUserService
	if exist, err := userServe.VerifyEmail(email); err != nil {
		return err
	} else {
		if exist {
			//邮箱已经存在
			var emailServe services.EmailService
			if err := emailServe.SendVerificationCodePhoneResetOldEmail(email); err != nil {
				return err
			} else {
				return nil
			}
		} else {
			//邮箱不存在
			return errors.New("该邮箱未注册!")
		}
	}
}

// 发送手机验证码
func SendPhoneCodeHandler(ctx iris.Context) {
	var RequestParams SendVerificationCodeRequest
	if err := ctx.ReadJSON(&RequestParams); err != nil {
		handler.ArgumentErrorHandle(err, ctx)
		return
	}
	var err error
	if RequestParams.RequestType == "1" {
		//修改密码
		err = sendPhoneVerificationCodePhoneResetPwd(RequestParams.Phone)
	} else if RequestParams.RequestType == "2" {
		//修改邮箱，旧手机号
		err = sendPhoneVerificationCodeEmailResetOldPhone(RequestParams.Phone)
	} else if RequestParams.RequestType == "3" {
		//修改手机号，旧手机号
		err = sendPhoneVerificationCodePhoneResetOldPhone(RequestParams.Phone)
	} else if RequestParams.RequestType == "4" {
		//修改手机号，新手机号
		err = sendPhoneVerificationCodePhoneResetNewPhone(RequestParams.Phone)
	} else {
		ctx.JSON(utils.ResponseServerError(errors.New("发送手机验证码类型异常！")))
		return
	}

	if err != nil {
		ctx.JSON(utils.ResponseServerError(err))
		return
	} else {
		ctx.JSON(utils.ResponseOK())
		return
	}
}

func sendPhoneVerificationCodePhoneResetPwd(phone string) error {
	var userServe services.SystemUserService
	if exist, err := userServe.VerifyPhone(phone); err != nil {
		return err
	} else {
		if exist {
			//手机号已经存在
			var phoneServe services.PhoneService
			if err := phoneServe.SendVerificationCodePhoneResetPwd(phone); err != nil {
				return err
			} else {
				return nil
			}
		} else {
			//手机号不存在
			return errors.New("该手机号未注册!")
		}
	}
}

func sendPhoneVerificationCodeEmailResetOldPhone(phone string) error {
	var userServe services.SystemUserService
	if exist, err := userServe.VerifyPhone(phone); err != nil {
		return err
	} else {
		if exist {
			//手机号已经存在
			var phoneServe services.PhoneService
			if err := phoneServe.SendVerificationCodeEmailResetOldPhone(phone); err != nil {
				return err
			} else {
				return nil
			}
		} else {
			//手机号不存在
			return errors.New("该手机号未注册!")
		}
	}
}

func sendPhoneVerificationCodePhoneResetOldPhone(phone string) error {
	var userServe services.SystemUserService
	if exist, err := userServe.VerifyPhone(phone); err != nil {
		return err
	} else {
		if exist {
			//手机号已经存在
			var phoneServe services.PhoneService
			if err := phoneServe.SendVerificationCodePhoneResetOldPhone(phone); err != nil {
				return err
			} else {
				return nil
			}
		} else {
			//手机号不存在
			return errors.New("该手机号未注册!")
		}
	}
}

func sendPhoneVerificationCodePhoneResetNewPhone(phone string) error {
	var userServe services.SystemUserService
	if exist, err := userServe.VerifyPhone(phone); err != nil {
		return err
	} else {
		if exist {
			//手机号已经存在
			return errors.New("该手机号已注册!")
		} else {
			//手机号不存在
			var phoneServe services.PhoneService
			if err := phoneServe.SendVerificationCodePhoneResetNewPhone(phone); err != nil {
				return err
			} else {
				return nil
			}
		}
	}
}
